package org.codegallery.javagal.grizzly;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.sun.grizzly.arp.AsyncExecutor;
import com.sun.grizzly.arp.AsyncFilter;
import com.sun.grizzly.cometd.standalone.CometdAdapter;
import com.sun.grizzly.http.embed.GrizzlyWebServer;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.grizzly.tcp.http11.GrizzlyAdapter;
import com.sun.grizzly.tcp.http11.GrizzlyRequest;
import com.sun.grizzly.tcp.http11.GrizzlyResponse;

public class AsyncEmbeddWebServerTest {
	public static void main(String[] args) {

	}

	private static void asyncCustomizedResource() {
		GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
		try {
			ws.addAsyncFilter(new AsyncFilter() {
				private final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(
						1);

				public boolean doFilter(final AsyncExecutor asyncExecutor) {
					// Throttle the request
					scheduler.schedule(new Callable() {
						public Object call() throws Exception {
							asyncExecutor.execute();
							asyncExecutor.postExecute();
							return null;
						}
					}, 10, TimeUnit.SECONDS);

					// Call the next AsyncFilter
					return true;
				}
			});

			ws.addGrizzlyAdapter(new GrizzlyAdapter() {
				public void service(GrizzlyRequest request,
						GrizzlyResponse response) {
					try {
						response.getWriter().println("Grizzly is soon cool");
					} catch (IOException ex) {
					}
				}
			});
			ws.start();
		} catch (IOException ex) {
			// Something when wrong.
		}
	}

	private static void asyncServlet() {
		GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
		try {
			ws.addAsyncFilter(new AsyncFilter() {
				private final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(
						1);

				public boolean doFilter(final AsyncExecutor asyncExecutor) {
					// Throttle the request
					scheduler.schedule(new Callable() {
						public Object call() throws Exception {
							asyncExecutor.execute();
							asyncExecutor.postExecute();
							return null;
						}
					}, 10, TimeUnit.SECONDS);

					// Call the next AsyncFilter
					return true;
				}
			});

			ServletAdapter sa = new ServletAdapter();
			sa.setRootFolder("/Path/To/Exploded/War/File");
			sa.setServletInstance(new MyServlet());
			ws.addGrizzlyAdapter(sa);

			ws.start();
		} catch (IOException ex) {
			// Something when wrong.
		}
	}

	private static void asyncServletWithBayeux() {
		GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
		try {
			// Add Comet Support
			ws.addAsyncFilter(new CometAsyncFilter());

			// Add Bayeux support
			CometdAdapter cometdAdapter = new CometdAdapter();
			ws.addGrizzlyAdapter(cometdAdapter);

			ServletAdapter sa = new ServletAdapter();
			sa.setRootFolder("/Path/To/Exploded/War/File");
			sa.setServletInstance(new MyServlet());
			ws.addGrizzlyAdapter(sa);

			ws.start();
		} catch (IOException ex) {
			// Something when wrong.
		}
	}
}
